using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Windows.Forms;
using System.Xml.Schema;
using System.IO;
using System.Drawing;
using OpenComposite.Base;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.Repository
{
	[RepositoryItem(ItemType.KPI_Metric, "kpi_metrics", "metric_id")]
	public class KPIMetric : RepositoryItemBase
	{
		#region Initialization
		/// <summary>
		/// Initializes a new instance of the <see cref="KPIMetric"/> class.
		/// </summary>
		public KPIMetric()
			: base("metric_id", "metric_nm", "metric_desc", "metric_ntext", Resources.sNewKPIMetric)
		{

		}
		/// <summary>
		/// Initializes a new instance of the <see cref="KPIMetric"/> class.
		/// </summary>
		/// <param name="bCreateNew">if set to <c>true</c> [b create new].</param>
		public KPIMetric(bool bCreateNew)
			: this()
		{
			if ( bCreateNew )
				this.CreateNew();
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="KPIMetric"/> class.
		/// </summary>
		/// <param name="id">The id.</param>
		public KPIMetric(int id)
			: this()
		{
			this.Load(id);
		}
		#endregion

		#region Properties

		#region Override
		/// <summary>
		/// Gets the image.
		/// </summary>
		/// <value>The image.</value>
		public override Image Image
		{
			get { return Resources.png_kpi; }
		}

		/// <summary>
		/// Gets the type of the repository item.
		/// </summary>
		/// <value>The type of the repository item.</value>
		public override ItemType RepositoryItemType
		{
			get { return ItemType.KPI_Metric; }
		}
		/// <summary>
		/// Gets the name of the repository item type.
		/// </summary>
		/// <value>The name of the repository item type.</value>
		public override string RepositoryItemTypeName
		{
			get { return "KPI Metric"; }
		}
		/// <summary>
		/// Gets the repository item type description.
		/// </summary>
		/// <value>The repository item type description.</value>
		public override string RepositoryItemTypeDescription
		{
			get { return ""; }
		}

		/// <summary>
		/// Gets the name of the table.
		/// </summary>
		/// <value>The name of the table.</value>
		public override string TableName
		{
			get { return "kpi_metrics"; }
		}
		private static List<ItemType> _LISTALLOWED = new List<ItemType>(new ItemType[] { ItemType.KPI_Function });
		/// <summary>
		/// Gets the allowed child types.
		/// </summary>
		/// <value>The allowed child types.</value>
		public override List<ItemType> AllowedChildTypes
		{
			get { return _LISTALLOWED; }
		}

		/// <summary>
		/// Gets the selected image.
		/// </summary>
		/// <value>The selected image.</value>
		public override Image SelectedImage
		{
			get { return this.Image; }
		}

		/// <summary>
		/// Gets the image key.
		/// </summary>
		/// <value>The image key.</value>
		public override string ImageKey
		{
			get { return "kpi"; }
		}

		/// <summary>
		/// Gets the selected image key.
		/// </summary>
		/// <value>The selected image key.</value>
		public override string SelectedImageKey
		{
			get { return this.ImageKey; }
		}

		/// <summary>
		/// Gets or sets the type of the data.
		/// </summary>
		/// <value>The type of the data.</value>
		public string DataType
		{
			get { return getString("metric_datatype", ""); }
			set { setValue("DataType", "metric_datatype", value); }
		}
		/// <summary>
		/// Gets or sets the value.
		/// </summary>
		/// <value>The value.</value>
		public string Value
		{
			get { return getString("metric_value", ""); }
			set { setValue("Value", "metric_value", value); }
		}
		/// <summary>
		/// Gets or sets the unit.
		/// </summary>
		/// <value>The unit.</value>
		public string Unit
		{
			get { return getString("metric_unit", ""); }
			set { setValue("Unit", "metric_unit", value); }
		}
		/// <summary>
		/// Gets or sets the kpi ID.
		/// </summary>
		/// <value>The kpi ID.</value>
		public int KpiID
		{
			get { return getInt("kpi_id", -1); }
			set { setValue("KpiID", "kpi_id", value); }
		}
		/// <summary>
		/// Gets or sets the kpi.
		/// </summary>
		/// <value>The kpi.</value>
		public KeyPerformanceIndicator Kpi
		{
			get { return Global.Data.RepositoryService.GetItem<KeyPerformanceIndicator>(this.KpiID); }
			set
			{
				if ( value == null ) KpiID = -1;
				else KpiID = value.ID;
			}
		}

		#endregion

		#endregion

		#region Methods

		#region Override
		/// <summary>
		/// Shows the new item form.
		/// </summary>
		/// <param name="idRepository">The repository ID.</param>
		/// <param name="bAllowContinue">if set to <c>true</c> to allow save and continue.</param>
		/// <param name="parentItem">The parent item.</param>
		/// <param name="idDomain">The domain ID.</param>
		/// <param name="parentItemNew">The new parent item.</param>
		/// <returns></returns>
		public override DialogResult ShowNewItemForm(int idRepository, bool bAllowContinue,
			IRepositoryItem parentItem, ref int idDomain, out IRepositoryItem parentItemNew)
		{
			KPI.FormKPIMetric dlg = new KPI.FormKPIMetric(this);
			DialogResult result = dlg.ShowDialog();

			// REMARK FormKPIMetric not based on FormCreateNewBase

			//if ( result == DialogResult.OK || result == DialogResult.Retry ) {
			//    idDomain = dlg.DomainID;
			//    idFolder = dlg.FolderID;
			//} else {
			idDomain = 0;
			parentItemNew = null;
			//}
			return result;
		}
		/// <summary>
		/// Sets the default values.
		/// </summary>
		protected override void SetDefaultValues()
		{
			base.SetDefaultValues();
		}
		/// <summary>
		/// Gets the icon.
		/// </summary>
		/// <returns></returns>
		public override Icon GetIcon()
		{
			Bitmap bmp = new Bitmap(this.Image);
			bmp.MakeTransparent(Color.Magenta);
			return Icon.FromHandle(bmp.GetHicon());
		}
		/// <summary>
		/// Copies this instance.
		/// </summary>
		/// <param name="deep"><c>true</c> to recursively copy the item and all subitems;
		/// <c>false</c> to copy only the item itself.</param>
		/// <returns>The copied item.</returns>
		public override IRepositoryItem Copy(bool deep)
		{
			KPIMetric copy = new KPIMetric(true);
			copy.InitializeServiceProvider(this);
			this.CopyDataRowToTarget(copy);
			return copy;
		}
		#endregion

		#endregion

	}
}